package com.lwz.cms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwz.cms.entity.CrmBanner;
import com.lwz.cms.service.CrmBannerService;
import com.lwz.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * admin操作banner（后台人员用于操作banner信息的接口）
 * @author Lw中
 * @since 2020-11-16
 */

@RestController
@RequestMapping("/cms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    CrmBannerService crmBannerService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit) {
        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        crmBannerService.page(pageBanner, null);
        return R.ok().data("items", pageBanner.getRecords()).data("total", pageBanner.getTotal());
    }

    /**
     * 通过id查询banner
     * @param id
     * @return
     */
    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = crmBannerService.getById(id);
        return R.ok().data("item", banner);
    }

    /**
     * 修改banner信息
     * @param banner
     * @return
     */
    @ApiOperation(value = "修改Banner")
    @CacheEvict(value = "banner", allEntries = true)
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        crmBannerService.updateById(banner);
        return R.ok();
    }

    /**
     * 删除banner
     * @param id
     * @return
     */
    @ApiOperation(value = "删除Banner")
    @CacheEvict(value = "banner", allEntries = true)
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        crmBannerService.removeById(id);
        return R.ok();
    }

    /**
     * 添加banner
     * @param crmBanner
     * @return
     */
    @ApiOperation(value = "添加banner")
    @CachePut(value = "banner", key = "'saveBanner'")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return R.ok();
    }

}

